package cn.edu.bjfu.it.oyss.covid_19helper;

import android.content.Intent;
import android.view.View;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import cn.edu.bjfu.it.oyss.covid_19helper.utils.ProvincesHelper;

import java.util.ArrayList;
import java.util.List;

public class ProvincesNameActivity extends AppCompatActivity {

    public static final String PROVINCES_ID_KEY = "provinces_id";
    private int counter;    //动态加载的计数器
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_provinces_name);

        ListView listView = findViewById(R.id.provincesNameList);
        List<String> provincesNameList = ProvincesHelper.getInstance(this).getProvincesNameList(this);
        final ArrayList<String> list = new ArrayList<>();
        counter = 0;
        int oldCounter = counter;
        for (counter = oldCounter; counter < oldCounter + 15; counter++) {
            list.add(provincesNameList.get(counter));
        }
        BaseAdapter adapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,list);
        listView.setAdapter(adapter);
        listView.setOnScrollListener(new AbsListView.OnScrollListener() {   //实现动态加载
                                         @Override
                                         public void onScrollStateChanged(AbsListView view, int scrollState) {

                                         }

                                         @Override
                                         public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                                             if (firstVisibleItem != 0 && counter<provincesNameList.size()) { // 不为0则表示有下拉动作 且 当当前数量小于总数时
                                                 if ((firstVisibleItem + visibleItemCount) > totalItemCount - 2) { // 当前第一个完全可见的item再下拉一个页面长度，即变为倒数第二个时
                                                     // 在此加载数据
                                                     int oldCounter = counter;
                                                     for (counter = oldCounter; counter < oldCounter + 15 && counter<provincesNameList.size(); counter++) {
                                                         list.add(provincesNameList.get(counter));
                                                     }
                                                     adapter.notifyDataSetChanged();
                                                 }
                                             }
                                         }
                                     });
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String provincesId = ProvincesHelper.getInstance(getParent()).getProvincesIdList(getParent()).get(position);
                Intent intent = new Intent(ProvincesNameActivity.this, StateActivity.class);
                intent.putExtra(PROVINCES_ID_KEY,provincesId);
                startActivity(intent);
                finish();
            }
        });
    }
}